package com.zcl.IO.buffered;

import org.junit.Test;

import java.io.*;

/**
 * @Description:
 * @author: zcl
 * @date: 2022/3/15  13:19
 * <p>
 * 处理流之一: 缓冲流的使用
 * BufferedInputStream:
 * BufferedOutputStream:
 * BufferedReader:
 * BufferedWriter:
 * <p>
 * 缓冲流的作用和意义:
 * 提高流的读取,写入的速度
 */
public class BufferedTest {


    @Test
    public void bufferedStreamTest() {
        BufferedInputStream bInput = null;
        BufferedOutputStream bOutput = null;
        try {
            //1.创建文件类对象
            File input = new File("src\\com\\zcl\\IO\\buffered\\p1.jpg");
            File output = new File("src\\com\\zcl\\IO\\buffered\\p2.jpg");

            //2.创建流
            FileInputStream fileInputStream = new FileInputStream(input);
            FileOutputStream fileOutputStream = new FileOutputStream(output);

            //3.创建处理流
            bInput = new BufferedInputStream(fileInputStream);
            bOutput = new BufferedOutputStream(fileOutputStream);

            //4.读写数据
            byte[] bytes = new byte[1024];
            int len;
            while ((len = bInput.read(bytes)) != -1) {
                bOutput.write(bytes, 0, len);
            }
        } catch (IOException ioException) {
            ioException.printStackTrace();
        } finally {

            //5.关闭流: 先关闭外层流,再关闭内层流
            //说明: 在关闭外层流的同时,内层的流也会自动关闭
            try {
                if (bOutput != null) {
                    bOutput.close();
                }
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            try {
                if (bInput != null) {
                    bInput.close();
                }
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
//        fileOutputStream.close();
//        fileInputStream.close();
        }
    }


    public void copyFile(String src, String dest) {
        BufferedInputStream bInput = null;
        BufferedOutputStream bOutput = null;
        try {
            //1.创建文件类对象
            File input = new File(src);
            File output = new File(dest);

            //2.创建流
            FileInputStream fileInputStream = new FileInputStream(input);
            FileOutputStream fileOutputStream = new FileOutputStream(output);

            //3.创建处理流
            bInput = new BufferedInputStream(fileInputStream);
            bOutput = new BufferedOutputStream(fileOutputStream);

            //4.读写数据
            byte[] bytes = new byte[1024];
            int len;
            while ((len = bInput.read(bytes)) != -1) {
                bOutput.write(bytes, 0, len);
            }
        } catch (IOException ioException) {
            ioException.printStackTrace();
        } finally {

            //5.关闭流: 先关闭外层流,再关闭内层流
            //说明: 在关闭外层流的同时,内层的流也会自动关闭
            try {
                if (bOutput != null) {
                    bOutput.close();
                }
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            try {
                if (bInput != null) {
                    bInput.close();
                }
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
//        fileOutputStream.close();
//        fileInputStream.close();
        }
    }
}
